package com.hxh.generater.springBoot.simple.gen;

import com.hxh.config.Constants;
import com.hxh.entity.Project;
import com.hxh.generater.springBoot.genBase.GenCache;
import com.hxh.generater.springBoot.util.ProcessWriteLine;
import com.hxh.util.GenUtil;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: AICode
 * @Package: com.hxh.generater
 * @Author: HXH
 * @Description:
 * @Date: 2023/11/28 15:08
 */
public class GenSimpleCache implements GenCache {

    @Override
    public void generationLocalCache(Project project)  {
        try {
            File classFile = GenUtil.newFile(project, "cache", "LocalCache");

            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\cache\\LocalCache");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            Map<String, String> replaceWord = new HashMap<>(1);
            replaceWord.put("GroupId", project.getBackendProject().getProjectGroup());
            ProcessWriteLine processWriteLine = new ProcessWriteLine(replaceWord);
            while ((line = br.readLine()) != null) {
                processWriteLine.writeLine(line, writer);
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
